﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LiveEduWizard.Views;
using LiveEduWizard.Filter;
using System.Collections.Specialized;

namespace LiveEduWizard
{
    public partial class AgendaItem : UserControl
    {
        private IFilterRule lastFilterRule = null;
        NotifyCollectionChangedEventHandler notifyCollectionChangedEventHandler;

        public AgendaItem()
            : this(null)
        {            
        }

        public AgendaItem(AgendaViewModel appointment)
        {
            notifyCollectionChangedEventHandler = new NotifyCollectionChangedEventHandler(Items_CollectionChanged);

            InitializeComponent();

            Agenda = appointment;
        }

        private AgendaViewModel agenda = null;
        public AgendaViewModel Agenda 
        {
            get { return agenda; }
            set
            {
                if (agenda != value)
                {
                    if (agenda != null)
                        agenda.Items.CollectionChanged -= notifyCollectionChangedEventHandler;
                    
                    agenda = value;

                    if (agenda != null)
                        agenda.Items.CollectionChanged += notifyCollectionChangedEventHandler;
                    
                    DataContext = agenda;

                    ApplyFilter(lastFilterRule);
                }
            }
        }

        private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ApplyFilter(lastFilterRule);
        }

        private double currentHeight = 0;
        public double CurrentHeight
        {
            get
            {
                if (Visibility == System.Windows.Visibility.Collapsed)
                    return 0;
                else
                    return currentHeight;
            }
        }
        
        private void Appointment_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender is FrameworkElement && ((FrameworkElement)sender).DataContext is AppointmentViewModel)
            {
                AppointmentView.Show(((FrameworkElement)sender).DataContext as AppointmentViewModel);
            }
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Height > 0)
                currentHeight = e.NewSize.Height;
        }

        public void ApplyFilter(IFilterRule rule)
        {
            LinkedList<AppointmentViewModel> appointments = new LinkedList<AppointmentViewModel>();

            if (agenda != null)
            {
                foreach (var item in agenda.Items)
                    if (rule == null || rule.IsMatch(item))
                        appointments.AddLast(item);
            }

            AppointmentsListBox.ItemsSource = appointments;
            Visibility = appointments.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
